package com.igeek.boot3_web_request.boot.controller;

/**
 * @Author: zhiyu
 * @Date:16:17
 * @DESC:
 */
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * 常见的请求格式：
 *  1.queryString: url?name=admin&password=123&age=20
 *  2.restful:     url/admin/123/20
 *  3.矩阵变量:     url/1;name=admin;password=123;age=20
 *
 *  测试三种路径：
 *      url/1;name=admin;password=123;age=20
 *      url/1;name=admin;password=123;age=20,30
 *      url/1;name=admin;password=123;age=20;age=30
 *
 *  定制SpringMVC:
 *  1.@Configuration+ @Bean 替换底层组件
 *  2.@Configuration + implements WebMvcConfigurer 重写部分方法
 *
 *  矩阵变量的使用：
 *  1.定制配置  @Configuration + implements WebMvcConfigurer 重写configurePathMatch方法
 *  2.矩阵变量的值是放置在rest路径中 /testMatrix/{user}
 *
 */
@Controller
public class MatrixController {

    //测试路径1：localhost:8084/testMatrix/1;name=admin;password=123;age=20
    //测试路径2：localhost:8084/testMatrix/1;name=admin;password=123;age=20,30
    //测试路径3：localhost:8084/testMatrix/1;name=admin;password=123;age=20;age=30
    @GetMapping("/testMatrix/{user}")
    @ResponseBody
    public Map<String,Object> testMatrix(
            @MatrixVariable(name = "name",required = false,defaultValue = "",pathVar = "user") String name,
            @MatrixVariable(name = "password",required = false,defaultValue = "",pathVar = "user") String password,
            @MatrixVariable(name = "age",required = false,defaultValue = "",pathVar = "user") int[] age
    ){
        HashMap<String, Object> map = new HashMap<>();
        map.put("name",name);
        map.put("password",password);
        map.put("age", Arrays.toString(age));
        return map;
    }

    //测试路径4：localhost:8084/testMatrix2/1;name=admin;password=123;age=20;/2;name=zs;password=666;age=30
    @GetMapping("/testMatrix2/{user}/{emp}")
    @ResponseBody
    public Map<String,Object> testMatrix2(
            @MatrixVariable(name = "name",required = false,defaultValue = "",pathVar = "user") String userName,
            @MatrixVariable(name = "password",required = false,defaultValue = "",pathVar = "user") String userPwd,
            @MatrixVariable(name = "age",required = false,defaultValue = "",pathVar = "user") int userAge,
            @MatrixVariable(name = "name",required = false,defaultValue = "",pathVar = "emp") String empName,
            @MatrixVariable(name = "password",required = false,defaultValue = "",pathVar = "emp") String empPwd,
            @MatrixVariable(name = "age",required = false,defaultValue = "",pathVar = "emp") int empAge
    ){
        HashMap<String, Object> map = new HashMap<>();
        map.put("userName",userName);
        map.put("userPwd",userPwd);
        map.put("userAge", userAge);
        map.put("empName",empName);
        map.put("empPwd",empPwd);
        map.put("empAge", empAge);
        return map;
    }
}